package httproxy

import (
	"http_proxy_v2/internal/balance"
	"net/http"
	"sync"
)

type UpStreamGroup struct {
	Name         string           `json:"name"` // 负载方案 robbin， random，ip_hash，weight
	UpStreamInfo []UpClient       `json:"up_stream_info"`
	balance      *balance.Service `json:"balance"`
}

//
type Rewrite struct {
	Regexp string `json:"regexp"` // uri 里配置的正则
	Value  string `json:"value"`  // 替换的值
}

type Location struct {
	Type     string        `json:"typ"`    // domain、uri 匹配，header头, uri参数 uri匹配，左匹配
	Method   string        `json:"method"` // match value， = value，in value，!= value
	Value    string        `json:"value"`  // typ匹配的结果 in 做split
	Up       UpStreamGroup `json:"up"`     // 对应的upstreams
	ConnInfo ConnectInfo
}

type StaticInfo struct {
	Root string `json:"root"` //实际路径
	Path string `json:"path"` //uri
}

type Proxy struct {
	Port       int        `json:"port"`
	StaticInfo StaticInfo `json:"static_info"`
	Rewrites   []Rewrite  `json:"rewrites"` // 路由重写(匹配一次即return，避免无限循环)
	Locations  []Location `json:"locations"`
}

type HttpProxy struct {
	DomainInfo  string `json:"domain_info" toml:"domain_info"`
	Port        int    `json:"port" toml:"port"`
	IsSSL       bool   `json:"is_ssl" toml:"is_ssl"`
	SSLCertPath string `json:"ssl_cert_path" toml:"ssl_cert_path"`
	SSLKeyPath  string `json:"ssl_key_path" toml:"ssl_key_path"`
	StaticRoot  string `json:"static_root" toml:"static_root"`
	StaticPath  string `json:"static_path" toml:"static_path"`
	BalanceName string `json:"balance_name" toml:"balance_name"`
}

type UpstreamServer struct {
	ID        int    `json:"id"`
	NetWork   string `json:"net_work" toml:"net_work"` //http协议
	Addr      string `json:"addr" toml:"addr"`
	Weight    int    `json:"weight" toml:"weight"`         // 0-100 more is better
	TimeOut   int    `json:"timeout" toml:"timeout"`       // 0-60s
	MaxConn   int    `json:"max_conn" toml:"max_conn"`     // 最大链接数
	FailTimes int    `json:"fail_times" toml:"fail_times"` // 最大失败次数，超过失败次数以后，该服务不再尝试
}

//与上游client配置
type UpClient struct {
	UpstreamServer
	Client http.Client
}

type ConnectInfo struct {
	Data    map[int]uint64
	ErrData map[int]uint64
	mu      sync.RWMutex
}
